import 'package:flutter/material.dart';

/// @date 2020/08/06  10:59
/// @author 程志强
/// @des 拖拽、滑动

class GestureDrag extends StatefulWidget {
  @override
  _GestureDragState createState() => _GestureDragState();
}

class _GestureDragState extends State<GestureDrag> {
  // 距离顶部的偏移
  double _top = 0.0;

  // 距离左边的偏移
  double _left = 0.0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("拖拽、滑动(任意方向)"),
      ),
      body: Stack(
        children: <Widget>[
          Positioned(
            top: _top,
            left: _left,
            child: GestureDetector(
              child: CircleAvatar(
                child: Text("A"),
              ),
              onPanDown: (DragDownDetails e) {
                // 手指按下时触发
                print("用户手指按下：${e.globalPosition}");
              },
              onPanUpdate: (DragUpdateDetails e) {
                //用户手指滑动时，更新偏移，重新构建
                setState(() {
                  _left += e.delta.dx;
                  _top += e.delta.dy;
                });
              },
              onPanEnd: (DragEndDetails e) {
                //打印滑动结束时在x、y轴上的速度
                print("==滑动速度==${e.velocity}");
              },
            ),
          )
        ],
      ),
    );
  }
}
